import { getRefundRule } from '../../config/https'; //推广、规则

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bgimg:'../../img/imgfenx.png',
    img:'',
    promote: '', //推广、规则 数据
  },

  rule(){ //推广、规则 数据
  let this_=this;
    
  },
  //点击开始的时间  
  timestart: function (e) {
    var _this = this;
    _this.setData({ timestart: e.timeStamp });
  },
  //点击结束的时间
  timeend: function (e) {
    var _this = this;
    _this.setData({ timeend: e.timeStamp });
  },

 
  //长按保存图片
  saveImg(e) {
    let url = this.data.img;
    //用户需要授权
    wx.getSetting({ 
      success: (res) => { //会出现小程序已经向用户请求过的权限
        console.log(res)
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({ //获取用户保存到相册权限
            scope: 'scope.writePhotosAlbum',
            success: () => {
              // 同意授权
              this.saveImg1(url);
            },
            fail: (res) => {
              console.log(res);
            }
          })
        } else {
          // 已经授权了
          this.saveImg1(url);
        }
      },
      fail: (res) => {
        console.log(res);
      }
    })
  },
  saveImg1(url) {
    wx.getImageInfo({ //获取图片信息
      src: url, //保存的画布图片链接
      success: (res) => {
        console.log(res)
        let path = res.path; //返回的图片本地路径
        wx.saveImageToPhotosAlbum({ //保存图片到系统相册
          filePath: path,
          success: (res) => {
            console.log(res);
          },
          fail: (res) => {
            console.log(res);
          }
        })
      },
      fail: (res) => {
        console.log(res);
      }
    })
  },

  onLoad: function(options) { //生命周期函数--监听页面加载
    let that = this;
    //推广、规则 接口
    getRefundRule({  
      scene: wx.getStorageSync("userInfo"),//本人id
      pages: 'pages/tapswitching/tapswitching',   
    }).then(res => {
      console.log(res)
      that.setData({ promote: res })
      let qrcode = res.qrcode
      //下载文件资源到本地
      wx.downloadFile({ 
        url: res.promotion_backgroundimg, //返回的背景图
        success(res) {
          // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
          if (res.statusCode === 200) {
            // wx.playVoice({
            //   filePath: res.tempFilePath
            // })
            that.setData({
              filePath: res.tempFilePath
            })
            let fileth = res.tempFilePath
            wx.downloadFile({
              url: qrcode,
              success(res) {
                // 绘制背景海报到canvas
                var postersize = that.setCanvasSize(750);//动态设置画布大小
                const ctx = wx.createCanvasContext('shareCanvas')
                ctx.drawImage(fileth, 0, 0, postersize.w, postersize.h)

                var re = wx.getSystemInfoSync();
                var scale = 750 / 280;
                var width = re.windowWidth / scale;
                var height = width
                var leftscale = 750 / 240;
                var left = re.windowWidth / leftscale;
                console.log(re.windowWidth, leftscale)
                var topscale = 750 / 880;
                var top = re.windowWidth / topscale;
                ctx.drawImage(res.tempFilePath, left, top, width, height)
                ctx.draw()
                // ctx.fillStyle = " #52D4E3";
                setTimeout(() => {
                  // code_url = this.canvasToTempImage(); 
                  //获取临时缓存合成照片路径，存入data中
                  wx.canvasToTempFilePath({
                    canvasId: 'shareCanvas',
                    success: function (res) {
                      var tempFilePath = res.tempFilePath;
                      that.setData({
                        img: tempFilePath
                      })
                      console.log(tempFilePath)
                    },
                    fail: function (res) {
                      console.log(res);
                    }
                  });
                }, 1000);

              }
            })
           
          }
        }
      })

    })
    

  },

  
  onReady: function () { //生命周期函数--监听页面初次渲染完成

  },

 
  onShow: function () { //生命周期函数--监听页面显示

  },

 
  onHide: function () { //生命周期函数--监听页面隐藏

  },

  setCanvasSize: function (width) {
    var size = {};
    try {
      var res = wx.getSystemInfoSync();
      var scale = 750 / width;
      // var scale = 1
      var width = res.windowWidth / scale;
      var height = res.windowHeight / scale;;
      size.w = width;
      size.h = height;
    } catch (e) {
      // Do something when catch error
      console.log("获取设备信息失败" + e);
    }
    return size;
  },
  //点击图片进行预览，长按保存分享图片
  previewImg: function (e) {
    var img = this.data.img;
    let _this = this;
    //保存二维码到相册

    wx.saveImageToPhotosAlbum({
      filePath: img,
      success: function (res) {
        wx.showModal({
          content: '保存成功',
          confirmText: '确认',
          showCancel: false,
          success: function (res) {

          }
        });
      },
      fail: function (res) {
        wx.showModal({
          content: '保存失败',
          confirmText: '确认',
          showCancel: false,
          success: function (res) {

          }
        });
      }
    })
  },
  onUnload: function () { //生命周期函数--监听页面卸载

  },

  
  onPullDownRefresh: function () { //页面相关事件处理函数--监听用户下拉动作

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
    return {
      title: '邀请好友成为分销商，大家一起赚钱！！！', //转发标题
      path: '/pages/tapswitching/tapswitching?userid=' + wx.getStorageSync('userInfo'), //转发的页面路径  默认是当前页面 
      imageUrl: "",  //转发图片 默认当前页面图片
      success: (res) => {//转发成功

      }
    }
  }
})